<?php

namespace plugin\ycMassage\app\shop\controller;

use app\common\builder\FormBuilder;
use app\common\builder\ListBuilder;
use app\common\service\UploadService;
use plugin\ycMassage\app\BaseController;
use plugin\ycMassage\app\model\MassageAlarm;
use plugin\ycMassage\utils\enum\AlarmState;
use support\Request;
use think\facade\Db;

class AlarmController extends BaseController
{
    public function indexGetTable(Request $request)
    {
        $limit   = $request->get('limit', 20);
        $builder = new ListBuilder;
        $builder->addActionOptions('操作', [
            'width' => 160
        ]);
        $builder->addScreen('uid', 'input', 'UID', [
            'placeholder' => 'UID搜索'
        ]);
        $builder->addScreen('state', 'select', '状态', [
            'options' => AlarmState::getOptions()
        ]);
        $builder->pageConfig([
            'pageSize' => $limit
        ]);
        $builder->addRightButton('update', '处理', [
            'api'    => $this->pluginPrefix . '/shop/Alarm/update',
            'path' => '/Alarm/update',
            'type'      => 'modal'
        ], [], [
            'type' => 'danger',
            'link' => true
        ]);
        $builder->addColumn('id', '序号', [
            'width' => 90
        ])
        ->addColumn('role', '身份', [
            'width' => 100,
        ])
            ->addColumnEle('headimg', '头像', [
                'width' => 60,
                'params' => [
                    'type' => 'image',
                ],
            ])
            ->addColumn('nickname', '昵称', [
                'width' => 120,
            ])
            ->addColumn('trade', '订单号',[
                'width' => 200,
            ])
            ->addColumn('title', '服务',[
                'width' => 120,
            ])
            ->addColumn('address', '服务地址',[
                'minWidth' => 200,
            ])
            ->addColumn('position', '定位地址',[
                'minWidth' => 200,
            ])
            ->addColumn('latitude', '纬度',[
                'width' => 120,
            ])
            ->addColumn('longitude', '经度',[
                'width' => 120,
            ])
            ->addColumnEle('state', '状态', [
                'width' => 100,
                'params' => [
                    'type' => 'tags',
                    'options' => AlarmState::dictOptions()
                ],
            ])
            ->addColumn('handle_remarks', '备注');
        return $this->successRes($builder->create());
    }
    public function index(Request $request)
    {
        $page = $request->get('page', 1);
        $limit = $request->get('limit', 20);
        $where = [
            ['a.saas_appid', '=', $request->saas_appid]
        ];
        $uid = $request->get('uid');
        if ($uid) {
            $where[] = ['a.uid', '=', $uid];
        }
        if ($request->use_mch_id) {
            $where[] = ['a.mch_id', '=', $request->use_mch_id];
        } else {
            $where[] = ['a.shop_id', '=', $request->use_shop_id];
        }
        $state = $request->get('state');
        if ($state !== null) {
            $where[] = ['a.state', '=', $state];
        }
        $data = MassageAlarm::where($where)
            ->alias('a')
            ->join('MassageUser u', 'u.id = a.uid')
            ->join('massage_appointment_service s', 's.id = a.service_id','left')
            ->join('massage_orders o', 'o.id = s.orders_id','left')
            ->join('MassageMch mch', 'mch.id = a.mch_id','left')
            ->join('MassageShop shop', 'shop.id = a.shop_id','left')
            ->order('a.create_time desc')
            ->field('a.*,u.nickname,u.headimg,mch.title as mch_title,shop.title as shop_title,o.trade')
            ->paginate([
                'list_rows' => $limit,
                'page' => $page
            ])->each(function ($item) {
                if ($item->nickname) {
                    $item->nickname = base64_decode($item->nickname);
                }
                if (strpos($item->headimg, 'http') !== 0) {
                    $item->headimg = UploadService::url($item->headimg);
                }
                $role='客户';
                if($item->use_role==='technician'){
                    $role='技师';
                }
                $item->role=$role;
            });
        return $this->successRes($data);
    }
    public function update(Request $request)
    {
        $id = $request->get('id');
        $MassageAlarm = MassageAlarm::where(['id' => $id, 'saas_appid' => $request->saas_appid])->find();
        if (!$MassageAlarm) {
            return $this->fail('数据不存在');
        }
        if ($request->method() === 'POST') {
            $D = $request->post();
            $MassageAlarm->state = $D['state'];
            Db::startTrans();
            try {
                $MassageAlarm->handle_remarks = $D['handle_remarks'];
                $MassageAlarm->handle_uid = $request->uid;
                $MassageAlarm->save();
                Db::commit();
                return $this->success('处理成功');
            } catch (\Throwable $th) {
                Db::rollback();
                return $this->fail($th->getMessage());
            }
        }

        $builder = new FormBuilder;
        $builder->setMethod('POST');
        $builder->addRow('state', 'radio', '状态', AlarmState::PROCESSED['value'], [
            'col' => 24,
            'options' => AlarmState::getOptions()
        ]);
        $builder->addRow('handle_remarks', 'textarea', '备注', '', [
            'col' => 24
        ]);
        $data = $builder->setData($MassageAlarm)->create();
        return $this->successRes($data);
    }
}
